﻿using BITUI.Tile;
using SDL2;
using System;
using System.Collections.Generic;
using System.Text;

namespace BITUI.Element
{
    public enum PanelADock
    {
        LeftTile,
        LeftPercent,
        TopTile,
        TopPercent,
        RightTile,
        RightPercent,
        BottomTile,
        BottomPercent,
    }
    //SplitPanel 和Canvas行为完全一致，除了他的子物体不能自由添加
    public class SplitPanel : Canvas //他和
    {
        public SplitPanel() : base()
        {
            this.panelA = new Canvas();
            this.panelA.Fill = false;
            this.panelB = new Canvas();
            this.panelB.Fill = false;
            base.AddChild(panelA);
            base.AddChild(panelB);
        }
        public PanelADock dockMode = PanelADock.LeftPercent;
        public int dockValue = 50;
        public int dockSplitSize = 1;
        public int dockBorderSize = 0;
        public Canvas panelA
        {
            get;
            private set;
        }
        public Canvas panelB
        {
            get;
            private set;
        }
        public override void AddChild(IElement2D element)
        {
            throw new Exception("not allowd");
        }
        public override void RemoveChild(IElement2D element)
        {
            throw new Exception("not allowd");
        }
        public override void OnDraw(IWindowRender render)
        {
            base.OnDraw(render);
        }
        public override void OnParentResize(int TileWidth, int TileHeight)
        {
            if (this.Fill)
            {
                this.TileWidth = TileWidth - this.TileXLocal - this.BorderRightLocal;
                this.TileHeight = TileHeight - this.TileYLocal - this.BorderBottomLocal;
            }
            //水平系
            if (dockMode == PanelADock.LeftPercent || dockMode == PanelADock.LeftTile || dockMode == PanelADock.RightPercent || dockMode == PanelADock.RightTile)
            {
                panelA.TileYLocal = dockBorderSize;
                panelA.TileHeight = (this.TileHeight - dockBorderSize * 2);

                panelB.TileYLocal = dockBorderSize;
                panelB.TileHeight = (this.TileHeight - dockBorderSize * 2);

                var totalwidth = (this.TileWidth - dockBorderSize * 2 - dockSplitSize);

                if (dockMode == PanelADock.LeftPercent || dockMode == PanelADock.RightPercent)
                {
                    panelA.TileWidth = (int)(totalwidth * (dockValue / 100.0));
                }
                else
                {
                    panelA.TileWidth = dockValue;
                }

                panelB.TileWidth = totalwidth - panelA.TileWidth;

                if (dockMode == PanelADock.LeftPercent || dockMode == PanelADock.LeftTile)
                {
                    panelA.TileXLocal = dockBorderSize;
                    panelB.TileXLocal = this.TileWidth - dockBorderSize - panelB.TileWidth;
                }
                else
                {
                    panelA.TileXLocal = this.TileWidth - dockBorderSize - panelA.TileWidth;
                    panelB.TileXLocal = dockBorderSize;

                }
            }
            //垂直系
            else
            {
                panelA.TileXLocal = dockBorderSize;
                panelA.TileWidth = (this.TileWidth - dockBorderSize * 2);

                panelB.TileXLocal = dockBorderSize;
                panelB.TileWidth = (this.TileWidth - dockBorderSize * 2);
                var totalheight = (this.TileHeight - dockBorderSize * 2 - dockSplitSize);


                if (dockMode == PanelADock.TopPercent || dockMode == PanelADock.BottomPercent)
                {
                    panelA.TileHeight = (int)(totalheight * (dockValue / 100.0));
                }
                else
                {
                    panelA.TileHeight = dockValue;
                }

                panelB.TileHeight = totalheight - panelA.TileHeight;

                if (dockMode == PanelADock.TopPercent || dockMode == PanelADock.TopTile)
                {
                    panelA.TileYLocal = dockBorderSize;
                    panelB.TileYLocal = this.TileHeight - dockBorderSize - panelB.TileHeight;
                }
                else
                {
                    panelA.TileYLocal = this.TileHeight - dockBorderSize - panelA.TileHeight;
                    panelB.TileYLocal = dockBorderSize;

                }
            }

            this.panelA.OnParentResize(this.TileWidth, this.TileHeight);
            this.panelB.OnParentResize(this.TileWidth, this.TileHeight);
        }
    }
}
